package com.cloudkinto.controller.headercontainer;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.firstvesselorder.FirstVesselOrderService;
import com.cloudkinto.service.headercontainer.HeaderContainerOrderService;
import com.cloudkinto.service.headercontainer.HeaderContainerService;
import com.cloudkinto.service.headercontainer.vo.*;
import com.cloudkinto.service.orderinput.OrderInputService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangyy
 * @since 2020-10-26
 */
@RestController
@Api(tags = "头程拼柜模块")
@RequestMapping("/api/admin/headerContainer")
public class HeaderContainerController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(HeaderContainerController.class);

    @Autowired
    private HeaderContainerService service;
    @Autowired
    private HeaderContainerOrderService orderService;
    @Autowired
    private FirstVesselOrderService firstVesselOrderService;
    @Autowired
    private OrderInputService orderInputService;

    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated HeaderContainerAddReq req) {
        return SingleResult.success("新增成功", service.add(req, getCurrentUserId()));
    }

    @UserSessionRequired
    @PostMapping("jointContainer")
    @ApiOperation(value = "拼柜", notes = "新增")
    public SingleResult jointContainer(@RequestBody @Validated HeaderContainerDetailAddReq req) {
        service.jointContainer(req, true);
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("autoJointContainer")
    @ApiOperation(value = "自动拼柜", notes = "新增")
    public SingleResult autoJointContainer(@RequestBody @Validated @NotEmpty List<Long> ids) {
        service.autoJointContainer(ids);
        return SingleResult.success();
    }

    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated HeaderContainerUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        return SingleResult.success("删除成功", service.delete(id, getCurrentUserId()));
    }

    @UserSessionRequired
    @PostMapping("deleteVesselOrder")
    @ApiOperation(value = "删除单个头程拼柜的订单", notes = "根据id删除")
    public SingleResult deleteVesselOrder(@RequestBody HeaderContainerDeleteVesselOrderReq req) {
        return SingleResult.success("删除成功", service.deleteVesselOrder(req, getCurrentUserId()));
    }

    @UserSessionRequired
    @GetMapping("/updateStatusInit")
    @ApiOperation(value = "编辑状态初始化", notes = "新增初始化")
    public SingleResult updateStatusInit() {
        return service.updateStatusInit();
    }

    @UserSessionRequired
    @PostMapping("/updateStatus")
    @ApiOperation(value = "编辑状态", notes = "新增初始化")
    public SingleResult updateStatus(@RequestBody @Validated HeaderContainerUpdateStatusReq req) {
        return service.updateStatus(req, getCurrentUserId());
    }

    @UserSessionRequired
    @PostMapping("/updateLicenceDate")
    @ApiOperation(value = "更新许可日期", notes = "更新许可日期")
    public SingleResult updateLicenceDate(@RequestBody @Validated HeaderContainerUpdateLicenceDateReq req) {
        return service.updateLicenceDate(req);
    }

    @UserSessionRequired
    @PostMapping("/updateInStorageDate")
    @ApiOperation(value = "更新入仓日期", notes = "更新入仓日期")
    public SingleResult updateInStorageDate(@RequestBody @Validated HeaderContainerUpdateInStorageReq req) {
        return service.updateInStorageDate(req);
    }

    @UserSessionRequired
    @PostMapping("/updateClearFileId")
    @ApiOperation(value = "更新入仓日期", notes = "更新入仓日期")
    public SingleResult updateClearFileId(@RequestBody @Validated HeaderContainerUpdateClearFileReq req) {
        return service.updateClearFileId(req);
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/setPriceInit")
    @ApiOperation(value = "设置费用初始化", notes = "查询初始化")
    public SingleResult setPriceInit(int type,long id) {
        return service.setPriceInit(type,id);
    }

    @UserSessionRequired
    @PostMapping("/setPrice")
    @ApiOperation(value = "设置费用", notes = "查询初始化")
    public SingleResult setPrice(@RequestBody HeaderFeeSetRequest request) {
        return service.setPriceItem(request);
    }

    @UserSessionRequired
    @PostMapping("/setValueAndRate")
    @ApiOperation(value = "设置货值和关税比", notes = "查询初始化")
    public SingleResult setValueAndRate(@RequestBody HeaderContainerValueAndRateReq req) {
        return orderService.setValueAndRate(req.getOrderId(), req.getValue(), req.getRate());
    }

    @UserSessionRequired
    @GetMapping("/setValueAndRateInit")
    @ApiOperation(value = "设置货值和关税比初始化", notes = "查询初始化")
    public SingleResult setValueAndRateInit(Long orderId) {
        return orderService.setValueAndRateInit(orderId);
    }


    @UserSessionRequired
    @GetMapping("/exportHeaderFeePage")
    @ApiOperation(value = "导出头程账单明细", notes = "查询初始化")
    public void exportHeaderFeePage(Long orderId, HttpServletResponse response) {
        service.exportHeaderFeePage(orderId, response);
    }

    @UserSessionRequired
    @GetMapping("/exportPackageInvoice")
    @ApiOperation(value = "导出入库清单", notes = "导出入库清单")
    public void exportPackageInvoice(Long id, HttpServletResponse responseBody) {
        service.exportPackageInvoice(id, responseBody);
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字搜索"), @ApiImplicitParam(name = "status", value = "当前状态")
            , @ApiImplicitParam(name = "current", value = "current"), @ApiImplicitParam(name = "pageSize", value = "pageSize")})
    public PageResult listPage(HttpServletRequest request) {

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String status = request.getParameter("status");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("keyword", keyword);
        map.put("status", status);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/FirstVesselOrderListPage")
    @ApiOperation(value = "未拼柜的头程订单查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult FirstVesselOrderListPage(HttpServletRequest request) {

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String keyword = request.getParameter("keyword");
        String storageId = request.getParameter("storageId");
        map.put("isContainer", SysConstant.DELETE_FLAG_FALSE.toString());
        map.put("current", current);
        map.put("pageSize", size);
        map.put("keyword", keyword);
        map.put("storageId", storageId);
//        return firstVesselOrderService.getListPage(map);
//        return orderInputService.getListPage(map);
        return service.getWaitOrderList(map);
    }

}

